package com.qf.controller;

import com.qf.util.CookieUtil;
import com.qf.util.JwtUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/30 21:21
 * description:
 */

@RestController
@RequestMapping("/auth")
public class LoginController {

    @Value("${custom.username}")
    private String username;
    @Value("${custom.password}")
    private String password;

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletResponse response){
        if(!StringUtils.isEmpty(username)&&!StringUtils.isEmpty(password)){
            //如果账号和密码匹配，则登录成功
            if(username.equals(this.username)&&password.equals(this.password)){
                System.out.println("登录成功");
                String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), username, null);
                CookieUtil.addCookie(response,"localhost","/","jwtToken",jwt,-1,false);

                return "login success!";
            }

        }
        return "login error!";
    }




}
